<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2019/08/09 11:35
 * Desc: 户外活动
 */
class Model_Api_V2_Outdoor
{

    /**
     * 获取活动行程
     * @function
     * @param $params
     * @return array
     */
    public static function get_detail_content($params)
    {
        $default = array(
            'typeid' => '114',
            'productinfo' => 0,
            'onlyrealfield' => 1,
            'pc' => 0
        );

        $params = array_merge($default, $params);
        extract($params);
        $arr = DB::select()->from('outdoor_content')
            ->and_where('isopen', '=', 1)
            ->order_by('displayorder', 'ASC')
            ->execute()
            ->as_array();

        //扩展表数据
        $productid = $productinfo['id'];//产品id
        $ar        = DB::select()->from('outdoor_extend_field')->where('productid', '=', $productid)->execute()->as_array();

        $list = array();
        foreach ($arr as $v) {
            if ($v['columnname'] == 'tupian' || $v['columnname'] == 'linedoc') {
                continue;
            }
            if ($v['isrealfield'] == 1 && $v['columnname'] != 'content') {
                $content = !empty($productinfo[$v['columnname']]) ? $productinfo[$v['columnname']] : $ar[0][$v['columnname']];
                $content = $content ? $content : '';
                if (empty($content)) {
                    continue;
                }
            }


            $a                = array();
            $a['columnname']  = $v['columnname'];
            $a['chinesename'] = $v['chinesename'];
            if ($v['columnname'] == 'content' && $productinfo['isstyle'] == 1) {
                $detail = Model_Outdoor_Jieshao::detail($productinfo['id'], $productinfo['lineday']);
                foreach ($detail as &$d) {
                    $d['jieshao'] = Model_Api_V2_Xcx::filter_content($d['content']);
                    $d['content'] = Model_Api_V2_Xcx::filter_content($d['content']);
                }

                $a['content']  = $detail;
                $a['is_array'] = 1;
            } else if ($v['columnname'] == 'content' && $productinfo['isstyle'] == 0) {
                $content = $productinfo['content'];
                if (empty($content)) {
                    continue;
                }
                $a['is_array'] = 0;
                $a['content']  = Model_Api_V2_Xcx::filter_content($content);

            } else {
                $a['content'] = Model_Api_V2_Xcx::filter_content($content);
            }

            $list[] = $a;

        }
        return $list;
    }

    public static function search($params)
    {
        $page    = ($params['page'] - 1) * $params['pagesize'];
        $obj     = DB::select('a.*')->from(array('outdoor', 'a'))
            ->join(array('allorderlist', 'b'))
            ->on('a.id', '=', DB::expr('sline_b.aid'))
            ->on('b.typeid', '=', 114)
            ->where('a.ishidden', '=', 0);
        $total   = $obj->execute()->count();
        $outdoor = $obj->offset($page)->limit($params['pagesize'])->execute()->as_array();
        foreach ($outdoor as &$v) {
            $v['price']    = Model_Outdoor::get_minprice($v['id'], array('info' => $v));
            $v['attrlist'] = Model_Outdoor::line_attr($v['attrid']);
            $v['litpic']   = Model_Api_V2_System::uniapp_img($v['litpic'], 750, 400);
        }
        return array('total' => $total, 'list' => $outdoor);
    }

    /**
     * 通过pid获取属性列表
     * @param $pid
     * @return array
     */
    public static function get_attribute_by_pid($pid = 0)
    {
        return DB::select('id', array('attrname', 'name'))
            ->from('outdoor_attr')
            ->where('pid', '=', $pid)
            ->and_where('isopen', '=', 1)
            ->order_by(DB::expr('ifnull(displayorder,9999)'), 'asc')
            ->execute()
            ->as_array();
    }

    /**
     * 格式化户外日期
     * @param $data
     * @return array
     */
    public static function format_date($data)
    {
        $date            = array();
        $week            = array("日", "一", "二", "三", "四", "五", "六");
        $date['addtime'] = date('Y年m月d日', $data['starttime']);
        $date['days']    = ceil(($data['endtime'] - $data['starttime']) / 86400);
        $date['week']    = $week[date('w', $data['starttime'])];
        return $date;
    }

    /**
     * 游客字段
     * @param $id
     * @param $gather_pos_ids
     * @return array
     */
    public static function tourer_fields($id, $gather_pos_ids = '')
    {
        $fields = Model_Outdoor_Tourer_Desc::get_tourer_fields($id);
        foreach ($fields as &$item) {
            if ($item['fieldname'] == 'assemble_point') {
                if ($gather_pos_ids) {
                    $item['options'] = DB::query(Database::SELECT, "select id,name from sline_outdoor_pos where id in ($gather_pos_ids) order by id desc")->execute()->as_array();
                } else {
                    $item['options'] = array();
                }
            }
        }
        return $fields;
    }
}